package cn.hxh.page.controller;

import cn.hxh.page.util.ParseImageUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: HongXiaoHan
 * @Description
 * @Date: 2020/10/5 10:33
 */

//每日图片控制器
@Controller
public class DayImgController {


    //解析百度网站图片数据API
    @ResponseBody
    @RequestMapping("/img/parseBaiDu")
    public String beautiful(@RequestParam("queryWord") String queryWord,
                            HttpServletRequest request) {
        String remoteAddr = request.getRemoteAddr();
        System.out.println(remoteAddr);


        int page;
        page = (int) ((Math.random() * 30) + 1);
        String encode = URLEncoder.encode(queryWord);
        String str = null;
        String url = "https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=" + encode
                + "&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=2&ic=1&hd=&latest=&copyright=&word=" + encode
                + "&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&pn="
                + page + "&rn=30&gsm=1e&1588511270025=";
        try {
            URL urls = new URL(url);
            URLConnection urlConnection = urls.openConnection();
            //设置连接时间
            urlConnection.setConnectTimeout(5000);
            InputStream in = urlConnection.getInputStream();
            int len = 0;
            byte[] bytes = new byte[1024];
            StringBuffer stringBuffer = new StringBuffer();
            while ((len = in.read(bytes)) != -1) {
                stringBuffer.append(new String(bytes, 0, len));
            }
            String string = stringBuffer.toString();
            JSONObject jsonObject = JSON.parseObject(string);
            JSONArray data = jsonObject.getJSONArray("data");
            //图片随机对象变量
            int imgI = (int) (Math.random() * 30);
            Map map = (Map) data.get(imgI);
            String objURL = (String) map.get("objURL");
            if (objURL != null) {
                str = ParseImageUtil.parse(objURL);
            }
        } catch (Exception e) {
            System.out.println("解析失败");
        }
        return str;
    }

    //解析wall网站图片数据API
    @ResponseBody
    @RequestMapping("/img/parseWall")
    public String parseWall(@RequestParam("queryWord") String queryWord) {
        System.out.println(queryWord);
        String imgurl = null;
        int page = (int) ((Math.random() * 30) + 1);
        if (queryWord.equals("动漫")) {
            page = (int) ((Math.random() * 400) + 1);
        }
        if (queryWord.equals("可爱")) {
            page = (int) ((Math.random() * 60) + 1);
        }
        if (queryWord.equals("简约")) {
            page = (int) ((Math.random() * 80) + 1);
        }
        String url = "https://wall.alphacoders.com/search.php?search=" + queryWord + "&lang=Chinese&page=" + page;
        try {
            Document document = Jsoup.parse(new URL(url), 200000);
            Elements elementsByClass = document.getElementsByClass("thumb-container-big");
            int i = (int) (Math.random() * 30);
            String id = elementsByClass.get(i).attr("id");
            String[] imgId = id.split("_");
            Document document2 = Jsoup.parse(new URL("https://wall.alphacoders.com/big.php?i=" + imgId[1] + "&lang=Chinese"), 200000);
            imgurl = document2.getElementsByClass("main-content").attr("src");
        } catch (IOException e) {
            System.out.println("解析失败");
        }
        return imgurl;
    }
}
